using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
namespace Services
{

    public partial class Receipt_VoucherSalesService
    {
        readonly IReceipt_VoucherSalesEntity entity;
        readonly IVoucherEntity VoucherEntity;
        public Receipt_VoucherSalesService()
        {
            this.entity = new Receipt_VoucherSalesEntity();
            this.VoucherEntity = new VoucherEntity();

        }

        #region Main Method

        /// <summary>
        /// Save 
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public long Save(object _model)
        {
            try
            {
                var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                if (id == 0)
                {
                    return entity.Save((CoreData.Receipt_VoucherSales)_model, Table.Receipt_VoucherSales.ToString());
                }
                else
                {
                    return entity.Update((CoreData.Receipt_VoucherSales)_model, Table.Receipt_VoucherSales.ToString());
                }
            }
            catch { return -1; }
        }

        /// <summary>
        /// Get by ID
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public CoreData.Receipt_VoucherSales GetByID(long _id)
        {
            try
            {
                return entity.GetByCusTomSQL(String.Format(SQLCommand.GetReceiptvoucherSale, " Voucher.Type=0 and Receipt_VoucherSales.ID=" + _id)).FirstOrDefault();
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Voucher> GetList()
        {
            try
            {
                return VoucherEntity.GetByCusTomSQL(String.Format(SQLCommand.GetReceiptvoucherSale, " Voucher.IsDeleted ='false' and Voucher.Type=0 ")).ToList();
            }
            catch { return null; }

        }

        /// <summary>
        /// Get List with Linq
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Receipt_VoucherSales> GetListByLINQ(Func<CoreData.Receipt_VoucherSales, Boolean> _where)
        {
            try
            {
                return entity.GetMany(_where, Table.Receipt_VoucherSales.ToString()).ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// Get One with Linq
        /// </summary>
        /// <returns></returns>
        public CoreData.Receipt_VoucherSales GetOneByLINQ(Func<CoreData.Receipt_VoucherSales, Boolean> _where)
        {
            try
            {
                return entity.Get(_where, Table.Receipt_VoucherSales.ToString());
            }
            catch { return null; }
        }

        /// <summary>
        /// Get List with search string
        /// </summary>
        /// <returns></returns>
        public IList<CoreData.Voucher> GetList(string _searchstring)
        {
            //search theo tieu chi nao do         
            try
            {
                return VoucherEntity.GetByCusTomSQL(String.Format(SQLCommand.GetReceiptvoucherSale, "  Voucher.IsDeleted ='false' and " + _searchstring)).ToList();
            }
            catch { return null; }
        }


        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        public bool Delete(object _model)
        {
            try
            {
                entity.Delete((CoreData.Receipt_VoucherSales)_model, Table.Receipt_VoucherSales.ToString());
                return true;
            }
            catch { return false; }
        }


        #endregion



        #region Other Method

        public CoreData.Receipt_VoucherSales GetByVoucherID(long id)
        {
            //search theo tieu chi nao do         
            try
            {
                return entity.GetBySearchString(" VoucherID=" + id, Table.Receipt_VoucherSales.ToString()).FirstOrDefault();
            }
            catch { return null; }
        }

        #endregion

    }

}
